import json
import re
import hashlib
from django.contrib.auth.hashers import pbkdf2
from django.contrib.auth.hashers import make_password, check_password
from datetime import datetime, timedelta
# from apps.utils.generate_account_number import GenerateAccountNumber
from rest_framework import serializers
from rest_framework.validators import UniqueValidator
from django.contrib.auth import get_user_model
# from rest_framework_jwt.utils import jwt_encode_handler, jwt_payload_handler
from rest_framework_jwt.serializers import jwt_encode_handler, jwt_payload_handler
# from rest_framework_jwt.views import obtain_jwt_token
from django.db.models import Q
from django.db import transaction
from common.com_utils import Utils
from common.com_time import Time
from apps.games.models import LivePlatformInfoModel, PkModeInfoModel, LiveRoomInfoModel, PkDetailInfoModel


class LivePlatformInfoSerializers(serializers.ModelSerializer):
    """
    直播平台信息序列化类
    """

    @transaction.atomic
    def validate(self, attrs):
        if attrs.get('created_time', None) is None:
            attrs['created_time'] = Time.current_datetime_str()
        return attrs

    class Meta:
        model = LivePlatformInfoModel
        fields = "__all__"


class PkModeInfoSerializers(serializers.ModelSerializer):
    """
    竞赛模式信息序列化类
    """

    @transaction.atomic
    def validate(self, attrs):
        if attrs.get('created_time', None) is None:
            attrs['created_time'] = Time.current_datetime_str()
        return attrs

    class Meta:
        model = PkModeInfoModel
        fields = "__all__"


class LiveRoomInfoSerializers(serializers.ModelSerializer):
    """
    直播间信息序列化类
    """
    live_room_number = serializers.CharField(required=False, max_length=30, label="直播间号码",
                                             help_text="直播间号码",
                                             error_messages={
                                                 'required': '直播间号码不能为空',
                                                 'blank': '直播间号码不能为空',
                                             })

    @transaction.atomic
    def validate(self, attrs):
        if attrs.get('live_room_number', None) is None:
            attrs['live_room_number'] = Utils.generator_live_room_number(num_len=30)
        if attrs.get('created_time', None) is None:
            attrs['created_time'] = Time.current_datetime_str()
        return attrs

    class Meta:
        model = LiveRoomInfoModel
        fields = "__all__"


class PkDetailInfoSerializers(serializers.ModelSerializer):
    """
    竞赛详细信息序列化类
    """

    @transaction.atomic
    def validate(self, attrs):
        if attrs.get('created_time', None) is None:
            attrs['created_time'] = Time.current_datetime_str()
        return attrs

    class Meta:
        model = PkDetailInfoModel
        fields = "__all__"
